package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.GoodsCategories;
import com.caocaog.market.service.GoodsCategoriesService;
import com.caocaog.market.service.ShopGenericService;
import com.caocaog.market.vo.LabelAndValueVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.base.entity.groups.InsertGroup;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类
 *
 * @author chechen
 * @date 2022-04-13
 */
@Slf4j
@RestController
@Api(value = "categories", tags = GoodsCategoriesController.TABLE_MEANING)
@RequestMapping("/goods/goodsSort")
public class GoodsCategoriesController extends GenericController<GoodsCategories, GoodsCategoriesService> {
    public static final String TABLE_MEANING = "商品分类";

    private ShopGenericService shopGenericService;

    @Trim
    @ApiOperation("根据店铺id获取显示的商品分类")
    @GetMapping("/getList")
    public Result<List<GoodsCategories>> getList(@RequestParam("shopId") String shopId) {
        return Result.OK(super.service.getShowGoodsCategoriesByShopId(shopId));
    }

    @Trim
    @ApiOperation(TABLE_MEANING + PAGE)
    @GetMapping("/list")
    public Result<IPage<GoodsCategories>> queryPageList(GoodsCategories goodsCategories, QueryRequest queryRequest) {
        if (StringUtils.isBlank(goodsCategories.getShopId())) {
            List<String> shopIds = this.shopGenericService.getShopIds();
            if (shopIds == null)
                throw new JeecgBootException("未找到门店");
            goodsCategories.setShopId(StringUtils.join(shopIds, StringPool.COMMA));
        }
        Page<GoodsCategories> page = PageGenerator.createPageSort(queryRequest);
        return Result.OK(super.service.queryGoodsCategoriesPage(page, goodsCategories));
    }

    @Trim
    @ApiOperation(value = "通过shopId获取", notes = "管理员角色设置默认shopId-> " + CommonConstant.DEFAULT_SHOP_ID)
    @GetMapping("/querySortsByShopId")
    public Result<List<LabelAndValueVo>> queryByShopId(@RequestParam(required = false) String shopId) {
        GoodsCategories goodsCategories = new GoodsCategories().setShopId(shopId);
        if (StringUtils.isBlank(shopId))
            this.shopGenericService.dealWithShopId(GoodsCategories::setShopId, goodsCategories);
        return Result.OK(super.service.getCategoriesLV(goodsCategories.getShopId()));
    }

    @Trim
    @AutoLog(TABLE_MEANING + ADD)
    @ApiOperation(value = ADD, notes = "管理员角色设置默认shopId-> " + CommonConstant.DEFAULT_SHOP_ID)
    @PostMapping("/add")
    public Result<GoodsCategories> add(@RequestBody @Validated({InsertGroup.class}) GoodsCategories goodsCategories) {
        goodsCategories.clearFields();
        if (StringUtils.isBlank(goodsCategories.getShopId()))
            this.shopGenericService.dealWithShopId(GoodsCategories::setShopId, goodsCategories);
        return Result.OK(ADD_SUCCESS, super.service.insertGoodsCategories(goodsCategories));
    }

    @Trim
    @AutoLog(TABLE_MEANING + EDIT)
    @ApiOperation(value = EDIT, notes = "管理员角色设置默认shopId-> " + CommonConstant.DEFAULT_SHOP_ID)
    @PutMapping("/edit")
    public Result<GoodsCategories> edit(@RequestBody @Validated({UpdateGroup.class}) GoodsCategories goodsCategories) {
        goodsCategories.clearFields();
        if (StringUtils.isBlank(goodsCategories.getShopId()))
            this.shopGenericService.dealWithShopId(GoodsCategories::setShopId, goodsCategories);
        return Result.OK(EDIT_SUCCESS, super.service.updateGoodsCategories(goodsCategories));
    }

    @Trim
    @AutoLog(TABLE_MEANING + DELETE)
    @ApiOperation(DELETE)
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestParam String id) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteGoodsCategories(id));
    }

    @Trim
    @AutoLog(TABLE_MEANING + DELETE_BATCH)
    @ApiOperation(DELETE_BATCH)
    @DeleteMapping("/deleteBatch")
    public Result<Integer> deleteBatch(@RequestParam String ids) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteGoodsCategories(ids.split(StringPool.COMMA)));
    }

    @Trim
    @ApiOperation(GET_BY_ID)
    @GetMapping("/queryById")
    public Result<GoodsCategories> queryById(@RequestParam String id) {
        return Result.OK(super.service.getByIdCache(id));
    }

    @Trim
    @AutoLog(TABLE_MEANING + "显示状态开关更新")
    @ApiOperation("显示状态开关更新")
    @GetMapping("/isShowChange/{id}")
    public Result<GoodsCategories> isShowChange(@PathVariable("id") String id) {
        GoodsCategories goodsCategories = super.service.getByIdCache(id);
        goodsCategories.setIsShow(goodsCategories.getIsShow().equals("0") ? "1" : "0");
        return Result.OK(OPERATE_SUCCESS, super.service.updateGoodsCategories(goodsCategories));
    }


    @Autowired
    public void setShopGenericService(ShopGenericService shopGenericService) {
        this.shopGenericService = shopGenericService;
    }

}
